package tool.soft

import java.io.File
import command.CommandExec
import dao.{MatrixFileDataDao, MissionDao}
import play.api.mvc.{AnyContent, Request}
import shared.Pojo._
import tool.Pojo._
import tool.{FormTool, Tool}
import implicits.Implicits._
import models.Tables
import models.Tables.MissionRow
import org.joda.time.DateTime
import play.twirl.api.Html
import tool.plot.AllPlot._
import utils.Utils

import scala.collection.immutable.SeqMap
import akka.stream.Materializer
import play.api.data.FormBinding
import play.api.i18n.Messages
import play.api.libs.ws.WSClient
import tool.softT.AllSoftBT

/** Created by yz on 11/9/2020
  */
object AllSoft {

  val softs = List(
    PlsdaSoft,
    PreDealSoft,
    AnnoSoft,
    PcaSoft,
    DiffAnaSoft,
    DiffAnaMulSoft,
    CcaRdaSoft,
    HeatmapSoft,
    OplsdaSoft,
    BasicSoft,
    ZScoreSoft,
    BoxplotSoft,
    ViolinPlotSoft,
    KeggEnrichSoft,
    IPathSoft,
    EnrichSoft,
    ParCorSoft,
    MetMicCorSoft,
    DiagnoseSoft,
    RfSoft,
    SvmSoft,
    BorutaSoft,
    VennSoft,
    PreprocessSoft,
    LefseAnaSoft,
    LimmaAnaSoft,
    StampSoft,
    HorizontalBoxplotSoft
  )

  trait SoftBT extends AllSoftBT {

    def produceConfigFile(workspaceDir: File, index: Int)(implicit
        request: Request[AnyContent],
        formBinding: FormBinding
    ): Any = {
      producePlotConfigFile(workspaceDir, index)
    }

    def produceInput(workspaceDir: File, resultDir: File)(implicit dao: MyDao): Unit = {}

    def resultDeal(workspaceDir: File, resultDir: File)(implicit ws: WSClient, materializer: Materializer): Unit = {
      Tool.resultDeal(workspaceDir, resultDir)
    }

    def exec(workspaceDir: File, resultDir: File, index: Int)(implicit
        dao: MyDao,
        ws: WSClient,
        materializer: Materializer
    ): CommandExec[Any, Boolean] = {
      CommandExec()
        .exec { b =>
          produceInput(workspaceDir, resultDir)
          getCommandData(workspaceDir, resultDir, index)
        }
        .map { b =>
          resultDeal(workspaceDir, resultDir)
        }
    }

    def getHtml(missionName: String, missionId: Option[Int] = None)(implicit
        request: Request[AnyContent],
        messages: Messages
    ): Html

    def getResultHtml(missionId: Int)(implicit request: Request[AnyContent], messages: Messages): Html

    def getArg(implicit request: Request[AnyContent], messages: Messages, formBinding: FormBinding): ujson.Value

    def getMission(implicit
        request: Request[AnyContent],
        matrixFileDataDao: MatrixFileDataDao,
        messages: Messages,
        formBinding: FormBinding
    ): MissionRow

    def produceInputFile(
        workspaceDir: File
    )(implicit request: Request[AnyContent], dao: MyDao, formBinding: FormBinding): Any

    def data(implicit request: Request[AnyContent], formBinding: FormBinding): AnalysisArg

    def userId(implicit request: Request[AnyContent]) = Tool.getUserId

    def getResultFileName: String = "AllMet.csv"

  }

}
